$.views.converters({
	fgoodsStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in stats_goodsStatus){
			var goodsStatus = stats_goodsStatus[index];
			if(goodsStatus.dictCode == value){
				return goodsStatus.dictName;
			}
		}
		return  "未知类型("+value+")";
	},

	fapplyStatus : function(value){
		if(!value){
			return "";
		}
		for(index in stats_approves){
			var approves = stats_approves[index];
			if(approves.dictCode == value){
				return approves.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	fmoneyUnit : function(value){
		if(!value){
			return "";
		}
		for(index in stats_currencys){
			var currencys = stats_currencys[index];
			if(currencys.dictCode == value){
				return currencys.dictName;
			}
		}
		return  "未知类型("+value+")";
	}
});
var goodsStatsCtrl  = new XFApp.client();
XFApp.client.prototype.goodsStats = {
	successCode : "10000",
	page : null,
	hisPage : null,
	hisId : 0,
	cusPage : null,
	reportId : null,
	urlItems : {
		queryByPageURL : basePath+"/userCenter/goods/goodsStats/queryPage",
		delGoodsRUL : basePath +"/userCenter/goods/goodsRecord/delGoods",
		queryCustomerByPageURL : basePath + "/userCenter/goods/goodsRecord/queryCusPage",
		queryByHisPageURL : basePath + "/userCenter/goods/goodsStats/queryHistoryPage",
		uploadResultURL :basePath+"/userCenter/goods/goodsStats/uploadResult",
		exportExcelURL:basePath+"/userCenter/goods/excel/export"
	},
	
	queryPage : function(){
		_this = this;
		var params = $('#query_from').serialize().replace(/\+/g, "%20");
		if(!!_this.page){
			_this.page.page("remote",0,params);
		}else{
			_this.page = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.queryByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		  webDialogUtil.simpleAlert(data.msg, "温馨提示");
			        	}
			        	_this.displayPop();
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示");
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
		}
	},
	displayPop:function(){
		$("[data-toggle='popover']").popover();
		$("[data-toggle='popover']").on('shown.bs.popover', function (event) {
			$("[data-toggle='popover']").each(function(){
				if(event.currentTarget.id!=$(this).attr("id")){
					$(this).popover('hide');
				}
			});
		});
	},
	queryHisPage : function(id){
		_this = this;
		var params = {
			id :id
		}
		if(!!_this.hisPage){
			_this.hisPage.page("remote",0,params);
		}else{
			_this.hisPage = $("#history_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryByHisPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			$("#his_page_grid").html($("#hisDataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#his_page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示");
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示");
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
			_this.hisId = id;
		}
	},
	delGoods : function(id){
		webDialogUtil.simpleConfrimAlert('是否确认删除？', function(){
		    	$.ajax({
					url:_this.urlItems.delGoodsRUL,
					data:{id:id},//JSON.stringify(docket),
					dataType:'json',
					type:'post',
					success:function(data){
						if(data.code!=10000){
							webDialogUtil.simpleAlert(data.msg, "温馨提示");
						}else{
							_this.queryPage();
						}
					},
					error:function(){
						webDialogUtil.simpleAlert("删除出现异常,请联系开发人员！", "温馨提示");
					}
				});
		});
	},
	queryCusPage : function(){
		_this = this;
		var params = {
			docketCode : $("#customercodeFilter").val(),
			docketName : $("#customernameFilter").val()
		}
		
		if(!!_this.cusPage){
			_this.cusPage.page("remote",0,params);
		}else{
			
			_this.cusPage = $("#customer_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryCustomerByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		$("#customer_data_grid").html($("#cusDataTmp").render(pageResult.itemList));
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示");
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据异常，请联系开发人员！", "温馨提示");
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}
	},
	selCustomer : function(value){
		$("#customercode").val(value);
		$("#choose_customer_modal").modal('hide'); 
	},
	queryHistory : function(id){
		_this.queryHisPage(id);
		$("#choose_history_modal").modal('show'); 
	},
	uploadModal : function(id,path){
		this.reportId = id;
		if(!!path){
			webDialogUtil.simpleConfrimAlert("改商品已上传过检测报告，是否更新报告？", function(){
				$("#checkResult").val("");
				$("#upload_modal").modal('show'); 
			});
		}else{
			$("#checkResult").val("");
			$("#upload_modal").modal('show'); 
		}
		
	},
	exportExcel : function(){
		var _this = this;
		var params = [];
		$("#query_from input").each(function() {
			if(!!$(this).attr("name")){
				var param = {name:'',value:''};
				param.name=$(this).attr("name");
				param.value= $(this).val();
				params.push(param);
			}
		});
		$("#query_from select").each(function() {
			if(!!$(this).attr("name")){
				var param = {name:'',value:''};
				param.name=$(this).attr("name");
				param.value= $(this).val();
				params.push(param);
			}
		});
		
		webDialogUtil.simpleConfrimAlert("是否确认导出", function(){
			$.ajaxFileUpload({
				url : _this.urlItems.exportExcelURL,
				secureuri:false,
				dataType: 'json',
				data : params,
				success: function (data, status) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}
				},
				error: function (data, status, e) {
					webDialogUtil.simpleAlert("处理异常", "温馨提示");
				}
			});
		});
	},
	upload: function(){
		var _this = this;
		var params = [];
		var param = {
				name:"id",
				value:_this.reportId
		}
		params.push(param);
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			$.ajaxFileUpload({
				url : _this.urlItems.uploadResultURL,
				secureuri:false,
				fileElementId:'checkResult',//file标签的id
				dataType: 'json',
				data : params,
				success: function (data, status) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						$("#upload_modal").modal('hide'); 
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
					}
				},
				error: function (data, status, e) {
					webDialogUtil.simpleAlert("处理异常", "温馨提示");
				}
			});
		});
	},
	event : function(){
		_this = this;
		$("#goods_search").on("click",function(){
			_this.queryPage();
		});
		$("#customercode_choose").on("click",function(){
			$("#choose_customer_modal").modal();
		});
		$("#custom_serach").on("click",function(){
			_this.queryCusPage();
		});
	
		$("#custom_reset").on("click",function(){
			$("input[name='customercodeFilter']").val("");
			$("input[name='customernameFilter']").val("");
		});
		
		$("#docketstatus_sel").bsSuggest({
			idField: "dictCode",  
			keyField: "dictName",
			effectiveFields :["dictCode","dictName"],
			effectiveFieldsAlias:{dictCode:"编码",dictName:"名称"},
	        data: {
	           'value':stats_goodsStatus,
	        }
	   }).on('onSetSelectValue', function (e, keyword) {
		   $("#docketstatus").val(keyword.id);
	    })
	    .on('onUnsetSelectValue', function (e) {
	        $("#docketstatus").val("");
	    });
		$("#docketstatus_sel").on("blur",function(){
			if(!$(this).val()){
				 $("#docketstatus").val("");
			}
		});
	},
};
$(function(){
	goodsStatsCtrl.goodsStats.event();
	goodsStatsCtrl.goodsStats.queryPage();
});
